package com.example.back.controller;

import com.example.back.bean.*;
import com.example.back.dao.SystemDao;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

@Api(tags = "后台登录接口")
@RestController
public class loginController {
    @Autowired
    private SystemDao systemUserDao;

    @ApiOperation(value = "管理员登录接口", httpMethod = "POST")
    @RequestMapping(value = "login")
    public AuthInfo getLoginUserInfo(String name, String passWord) {
//        System.out.println("进入Controller层");
        SystemUser systemUser = systemUserDao.getUserInfo(name);
        AuthInfo authInfo = new AuthInfo();
        if (systemUser == null) {
            authInfo.setLoginStatus(400);
            return authInfo;
        } else {
            List<AuthUserInfoFather> authUserInfoFatherList = new ArrayList<AuthUserInfoFather>();
            if (systemUser.getId() > 0 && systemUser.getPassword().equals(passWord) && systemUser.getFailureNum() < 4 && systemUser.getStatus() == 0) {
                systemUserDao.updateLoginFailureNum(systemUser.getId());
                List<SystemRole> systemRoleList = systemUserDao.getSystemRoleInfo(systemUser.getId());
                Map<Integer, SystemFunction> systemFunctionHashMap = new HashMap<Integer, SystemFunction>();
                for (SystemRole systemRole : systemRoleList) {
                    List<SystemFunction> systemFunctionList = systemUserDao.getSystemFunction(systemRole.getId());
                    for (SystemFunction systemFunction : systemFunctionList) {
                        systemFunctionHashMap.put(systemFunction.getId(), systemFunction);
                    }
                }

                List<SystemFunction> finalFunctionList = new ArrayList<SystemFunction>();
                AuthUserInfoFather authUserInfo = new AuthUserInfoFather();
                Iterator<Map.Entry<Integer, SystemFunction>> keyIterator = systemFunctionHashMap.entrySet().iterator();
                SystemFunction function;
                while (keyIterator.hasNext()) {
                    Map.Entry<Integer, SystemFunction> entry = keyIterator.next();
                    function = systemFunctionHashMap.get(entry.getKey());
                    for (AuthUserInfoFather authUserInfoFather : authUserInfoFatherList) {
                        AuthUserInfo authUserInfo1 = new AuthUserInfo();
                        if (authUserInfoFather.getId() == entry.getValue().getSuperior()) {
                            authUserInfo1.setTitle(entry.getValue().getName());
                            authUserInfo1.setRouter(entry.getValue().getRoute());
                            authUserInfo1.setSuperior(entry.getValue().getSuperior());
                            authUserInfo1.setId(entry.getValue().getId());
                            authUserInfoFather.getChildren().add(authUserInfo1);
                        }
                    }
                    if (entry.getValue().getSuperior() == -1) {
                        List<AuthUserInfo> authUserInfoList = new ArrayList<>();
                        authUserInfo.setId(entry.getValue().getId());
                        authUserInfo.setRouter(entry.getValue().getRoute());
                        authUserInfo.setTitle(entry.getValue().getName());
                        authUserInfo.setSuperior(entry.getValue().getSuperior());
                        authUserInfo.setChildren(authUserInfoList);
                        authUserInfoFatherList.add(authUserInfo);
                    }


                    finalFunctionList.add(function);

                }

                authInfo.setFunctionList(finalFunctionList);
                authInfo.setAuthUserInfoFatherList(authUserInfoFatherList);
                authInfo.setRoleList(systemRoleList);
                authInfo.setLoginStatus(0);
                authInfo.setSessionId(UUID.randomUUID().toString().replace("-", ""));

            } else if (!systemUser.getPassword().equals(passWord) && systemUser.getFailureNum() < 4 && systemUser.getStatus() == 0) {
                systemUserDao.updateFailureNum(systemUser.getId(), systemUser.getFailureNum() + 1);
                authInfo.setLoginStatus(401);
                authInfo.setFailureNum(systemUser.getFailureNum() + 1);
            } else {
                systemUserDao.updateLoginStatus(systemUser.getId());
                authInfo.setLoginStatus(402);
            }


            return authInfo;
        }
    }

    @RequestMapping("/updatePassWord")
    public Map<String, Object> updatePassword(String name, String passWord) {
        Map<String, Object> map = new HashMap<>();
        if (systemUserDao.updatePassWord(name, passWord)) {
            map.put("code", 200);
            map.put("msg", "修改成功，请重新登录！！！");
        } else {
            map.put("code", 500);
            map.put("msg", "新密码不能与旧密码一致！！！");
        }
        return map;
    }

}
